import Vue from "vue";
import VueRouter from "vue-router";
import LayoutPage1 from "@/components/layout/LayoutPage1.vue";
import LayoutPage2 from "@/components/layout/LayoutPage2.vue";
import LayoutPage3 from "@/components/layout/LayoutPage3.vue";
import RouterViewBlock from "@/components/RouterView/Block.vue";
import UserLyout1 from "@/components/UserLyout/UserLyout1.vue";

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => {
    console.log(err);
  });
};

VueRouter.prototype.replace = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: { name: "Home" },
    component: LayoutPage1,
    children: [
      // 登录
      {
        path: "home",
        name: "Home",
        meta: { title: "道多多商城" },
        component: () => import("../pages/home/"),
      },
      // 登录
      {
        path: "login",
        name: "Login",
        meta: { title: "用户登录" },
        component: () => import("../pages/login/"),
      },
      {
        path: "login/:type",
        name: "Register",
        meta: { title: "用户注册" },
        component: () => import("../pages/login/"),
      },
      // 活动页面
      {
        path: "action",
        name: "Action",
        component: RouterViewBlock,
        children: [
          // 活动页面
          {
            path: "action",
            name: "ActionPage",
            meta: { title: "活动页面" },
            component: () => import("../pages/action/action"),
          },
          // 品牌活动
          {
            path: "brand",
            name: "ActionBrand",
            meta: { title: "品牌活动" },
            component: () => import("../pages/action/brand"),
          },
          // 秒杀活动
          {
            path: "spike",
            name: "ActionSpike",
            meta: { title: "秒杀活动" },
            component: () => import("../pages/action/spike"),
          },
          // 特价活动
          {
            path: "lowprice",
            name: "ActionLowprice",
            meta: { title: "特价活动" },
            component: () => import("../pages/action/lowprice"),
          },
        ],
      },
      {
        path: "goods",
        name: "Goods",
        component: RouterViewBlock,
        children: [
          // 商品推荐页面
          {
            path: "recommend",
            name: "GoodsRecommend",
            meta: { title: "商品推荐" },
            component: () => import("../pages/goods/recommend"),
          },
          // 商品详情
          {
            path: "details/:id",
            name: "GoodsDetails",
            meta: { title: "商品详情" },
            component: () => import("../pages/goods/details"),
          },
          // 商品搜索
          {
            path: "search",
            name: "GoodsSearch",
            meta: { title: "商品搜索" },
            component: () => import("../pages/goods/search"),
          },
        ],
      },
      // 店铺页面
      {
        path: "shop",
        name: "Shop",
        component: RouterViewBlock,
        children: [
          // 店铺详情
          {
            path: "details/:id",
            name: "ShopDetails",
            meta: { title: "店铺详情" },
            component: () => import("../pages/shop/details"),
          },
          // 店铺详情
          {
            path: "street",
            name: "ShopStreet",
            meta: { title: "店铺街" },
            component: () => import("../pages/shop/street"),
          },
          // 店铺搜索
          {
            path: "search",
            name: "ShopSearch",
            meta: { title: "店铺搜索" },
            component: () => import("../pages/shop/search"),
          },
        ],
      },
      // 用户页面
      {
        path: "user",
        name: "User",
        component: UserLyout1,
        children: [
          {
            path: "center",
            name: "UserCenter",
            meta: { title: "用户中心" },
            component: () => import("../pages/users/center"),
          },
          {
            path: "attent",
            name: "UserAttent",
            meta: { title: "店铺关注" },
            component: () => import("../pages/users/attent"),
          },
          {
            path: "data",
            name: "UserData",
            meta: { title: "用户资料" },
            component: () => import("../pages/users/data"),
          },
          {
            path: "feedback",
            name: "UserFeedback",
            meta: { title: "投诉建议" },
            component: () => import("../pages/users/feedback"),
          },
          {
            path: "demand",
            name: "UserDemand",
            meta: { title: "产品需求" },
            component: () => import("../pages/users/demand"),
          },
          {
            path: "pwd",
            name: "UserPwd",
            meta: { title: "修改密码" },
            component: () => import("../pages/users/pwd"),
          },
          {
            path: "addr",
            name: "UserAddr",
            meta: { title: "收货地址" },
            component: () => import("../pages/users/addr"),
          },
          {
            path: "coupon",
            name: "UserCoupon",
            meta: { title: "优惠券" },
            component: () => import("../pages/users/coupon"),
          },
          {
            path: "favo",
            name: "UserFavo",
            meta: { title: "我的收藏" },
            component: () => import("../pages/users/favo"),
          },
          // 订单列表
          {
            path: "order",
            name: "OrderList",
            meta: { title: "我的订单" },
            component: () => import("../pages/users/order"),
          },
          {
            path: "order/:type",
            name: "OrderList",
            meta: { title: "我的订单" },
            component: () => import("../pages/users/order"),
          },
          {
            path: "saleafter/:type",
            name: "SaleafterList",
            meta: { title: "我的订单" },
            component: () => import("../pages/users/saleafter"),
          },
        ],
      },
    ],
  },
  // 购物车
  {
    path: "/car",
    name: "Car",
    component: LayoutPage2,
    children: [
      // 店铺详情
      {
        path: "index",
        name: "CarIndex",
        meta: { title: "购物车" },
        component: () => import("../pages/car/index"),
      },
    ],
  },
  {
    path: "/order",
    name: "Order",
    component: LayoutPage3,
    children: [
      // 创建订单
      {
        path: "create",
        name: "OrderCreate",
        meta: { title: "创建订单" },
        component: () => import("../pages/order/create"),
      },
      // 订单详情
      {
        path: "details/",
        name: "OrderDetails",
        meta: { title: "订单详情" },
        component: () => import("../pages/order/details"),
      },
      {
        path: "details/:id",
        name: "OrderDetails",
        meta: { title: "订单详情" },
        component: () => import("../pages/order/details"),
      },
      // 订单详情
      {
        path: "saleafter/:id",
        name: "OrderSaleafter",
        meta: { title: "申请售后" },
        component: () => import("../pages/order/saleafter"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  window.scrollTo(0, 0);
  if (to.meta.title) {
    //判断是否有标题
    document.title = to.meta.title;
  }
  next();
});

export default router;
